var limit = 500;
layui.use(['laypage', 'table', 'form', 'laydate'], function(){
    var table = layui.table,laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#search-startTime',
        type: 'datetime'
    });
    laydate.render({
        elem: '#search-endTime',
        type: 'datetime'
    });

    let todoCols = [
        [
            {field: 'id', title: '标识', width:80, align: 'center', hide: true}
            ,{field: 'lineName', title: '生产线', width:120, sort:true, align: 'center'}
            ,{field: 'storageName', title: '仓位', width: 120, sort:true, align: 'center'}
            ,{field: 'nameNorms', title: '材料', width:120, sort:true, align: 'center'}
            ,{field: 'labQuantity', title: '配比用量(吨)', width:140, sort:true,totalRow:true, align: 'center'}
            ,{field: 'setQuantity', title: '工控设定量(吨)', width:120, sort:true,totalRow:true, align: 'center'}
            ,{field: 'realQuantity', title: '实际用量(吨)', width:120, sort:true,totalRow:true, align: 'center'}
            ,{field: 'errorValue', title: '误差值(吨)', width:120, sort:true,totalRow:true, align: 'center'}
            ,{field: 'errorRate', title: '误差率(吨)', width:120, sort:true,totalRow:true, align: 'center'}
            ,{field: 'type', title: '消耗类型', width:120, sort:true, align: 'center',templet:function (val) {
                if (val.type == 0)
                    return '自动';
                return '手动';
            }}
        ]
    ];

    table.render({
        elem: '#body-list'
        ,url: '/lab-expend/storage-expend'
        ,method: 'post'
        // ,page: true
        ,cols: todoCols
            ,totalRow:true
        // ,limit: limit
        // ,limits: [15,30,50,100]
        // ,request: {
        //     pageName: 'curr' //页码的参数名称，默认：page
        //     ,limitName: 'limit' //每页数据量的参数名，默认：limit
        // }
        ,where: {
            offset: 0,
            limit: limit,
            orgId: $('.search-origin').val(),
            startTime: $('.search-startTime').val(),
            endTime: $('.search-endTime').val()
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            console.log(res)
            if (res.code == 0 && res.data != null) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": 0, //解析数据长度
                    "data": res.data //解析数据列表
                };
            } else {
                return {
                    "code": res.code, //解析接口状态
                    "msg": '没有任何数据', //解析提示文本
                    "count": 0, //解析数据长度
                    "data": '' //解析数据列表
                };
            }
        }
    });

    form.on('select(search-line)', function(data) {
        $(".btn-query").trigger("click");
    });
    form.on('select(search-storage)', function(data) {
        $(".btn-query").trigger("click");
    });
    form.on('select(search-material)', function(data) {
        $(".btn-query").trigger("click");
    });
    $('.btn-query').on('click', function () {//搜索按钮
        var orgId = $('.search-origin').val();
        var lineId = $('.search-line').val();
        var storageId = $('.search-storage').val();
        var materialId = $('.search-material').val();
        var startTime = $('.search-startTime').val();
        var endTime = $('.search-endTime').val();
        var option = {page:1,limit:limit};
        if (!isNull(lineId))
            option['lineId'] = lineId;
        if (!isNull(storageId))
            option['storageId'] = storageId;
        if (!isNull(orgId))
            option['orgId'] = orgId;
        if (!isNull(materialId))
            option['materialId'] = materialId;
        if (!isNull(startTime))
            option['startTime'] = startTime;
        if (!isNull(endTime))
            option['endTime'] = endTime;
        console.log(option)
        table.reload('body-list', {
            where: option,page:{curr:1}
        });
    });
    $('.btn-export').on('click', function () {//导出报表
        var startTime = $('.search-startTime').val();
        var endTime = $('.search-endTime').val();
        var lineId = $('.search-line').val();
        var line = $('.search-line').find("option:selected").text();
        var storage = $('.search-storage').find("option:selected").text();
        var storageId = $('.search-storage').val();
        var orgId = $('.search-origin').val();
        var materialId = $('.search-material').val();
        var material = $('.search-material').find("option:selected").text();
        var orgName = $('.search-origin').find("option:selected").text();
        var tips = '';
        if (orgId == null) {
            orgId = -1;
            layer.msg('请选择导出公司~!');
            return;
        }
        if (isNull(startTime)) {
            layer.msg('请选择导出开始时间~!');
            return ;
        }
        if (isNull(endTime)) {
            layer.msg('请选择导出结束时间~!');
            return ;
        }
        var loading = layer.load(1);
        layer.confirm('是否导出' + startTime + ' 至 ' + endTime + '料仓消耗统计汇总表?', {
            btn: ['确定', '取消']
        }, function(index, layero){
            exportData('/lab-expend/storage-expend-export',{
                lineId:lineId,startTime:startTime,endTime:endTime,orgId:orgId,line:line,
                materialId:materialId,storage:storage,storageId:storageId,limit:500000,
                orgName:orgName,material:material
            },function () {
                // $(".btn-query").trigger("click");
            },null,null);
            layer.closeAll();
        }, function(index){

            layer.closeAll();
        });
    });
});